<template>
  <div class="waste-order">
    <van-nav-bar title="废品订单" left-arrow @click-left="this.$router.replace('/my')" />
    <div class="good-content">
      <van-tabs v-model:active="active">
        <van-tab v-for="item in lablist" :key="item.id" :title="item.name" @click-tab="onClickTab">
          <div class="good-card" v-for="item in cardlist" :key="item.id">
            <div class="left">
              <img src="../../assets/image/logo.jpg" alt="" />
            </div>
            <div class="main">
              <p class="name">{{ item.wasteid }}</p>
              <p class="time">{{ item.createTime }}</p>
            </div>
            <div class="right">
              <p class="price">{{ item.status === '0' ? '' : '￥'}}{{ item.price }}</p>
              <van-button plain hairline type="primary" v-if="item.status === '0'" @click="showDialog">催收</van-button>
            </div>
          </div>
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>

<script>
import { showToast, showDialog } from 'vant'
import { WasteOrder } from '../../api/user.js'
export default {
  data() {
    return {
      // tab的激活项
      active: Number(window.sessionStorage.getItem('wasteActive')),
      // active: 2,
      // 标签
      lablist: [
        { id: 1, name: '待上门' },
        { id: 2, name: '已回收' },
        { id: 3, name: '全部订单' }
      ],
      // 商品订单数据
      wastelist: [],
      // 渲染的商品卡片
      cardlist: []
    }
  },
  created() {
    this.getwastelist()
  },
  methods: {
    async getwastelist() {
      const { data: res } = await WasteOrder()
      if (res.code === 1) {
        this.wastelist = res.data
        if (this.$route.params.active !== '2') {
          this.cardlist = this.wastelist.filter(
            (item) => item.status === String(this.$route.params.active)
          )
        } else {
          this.cardlist = this.wastelist
        }
      } else {
        showToast('获取数据失败！')
      }
    },
    showDialog() {
      showDialog({
        message: '工作人员正在快马加鞭赶来，请耐心等候！'
      }).then(() => {
        this.showRule = false
      })
    }
  },
  watch: {
    active: {
      immediate: true,
      async handler(newVal) {
        switch (newVal) {
          case 0:
            this.cardlist = this.wastelist.filter((item) => item.status === '0')
            break
          case 1:
            this.cardlist = this.wastelist.filter((item) => item.status === '1')
            break
          case 2:
            this.cardlist = this.wastelist
            break
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.waste-order {
  width: 100%;
  height: 100vh;
  box-sizing: border-box;

  .good-content {
    // 商品卡片
    .good-card {
      display: flex;
      align-items: center;
      width: 95%;
      height: 100px;
      margin: 15px 10px;
      padding: 7px;
      border-radius: 10px;
      background-color: #fff;
      box-sizing: border-box;
      .left {
        width: 32%;
        height: 100%;
        // background-color: pink;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .main {
        width: 48%;
        height: 100%;
        margin-left: 3px;
        // background-color: skyblue;

        .name {
          font-size: 15px;
        }
        .time {
          color: #666;
          font-size: 14px;
        }
      }
      .right {
        height: 100%;
        // background-color: orange;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        align-items: start;

        .price {
          margin: 0;
          white-space: nowrap;
          font-size: 15px;
          margin-top: 3px;
        }

        .van-button {
          width: 70px;
          height: 30px;
          margin: 0;
          font-size: 15px;
        }
      }
    }
  }
}
</style>
